﻿using static Dm.net.buffer.ByteArrayBuffer;

namespace Jst.Core.jwt
{
    /// <summary>
    /// 后台回调实体
    /// </summary>
    public class ApiResult
    {
        /// <summary>
        /// 状态码(ok = 0, error = 1, timeout = 401)
        /// </summary>
        public int code { get; set; } = 1;

        public bool success { get; set; }

        public string message { get; set; } = null;

        public object data { get; set; }

        public string responseDatetime { get; set; }
        public object resultObject { get; set; }


        /// <summary>
        /// 设置API调用结果为成功
        /// </summary>
        /// <returns></returns>
        public ApiResult SuccessResult()
        {
            code = 0;
            responseDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            success = true;
            message = "success";
            resultObject = string.Empty;
            return this;
        }

        /// <summary>
        /// 设置API调用结果为成功
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public ApiResult Success(string message,object data) 
        {
            code = 0;
            responseDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            success = true;
            message = message;
            data = data;
            return this;
        }

        /// <summary>
        /// 设置API调用结果为成功
        /// </summary>
        /// <param name="result">不需要从Data里面读取返回值对象时，存储简单的值对象或者string</param>
        /// <returns></returns>
        public ApiResult SuccessResult(string result)
        {
            code = 0;
            responseDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            success = true;
            message = "success";
            resultObject = result;
            return this;
        }

        /// <summary>
        /// 设置API调用结果为失败
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <returns></returns>
        public ApiResult FailedResult(int errorCode, string errorMessage)
        {
            code = errorCode;
            responseDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            success = false;
            message = errorMessage;
            resultObject = string.Empty;
            return this;
        }

        /// <summary>
        /// 设置API调用结果为失败
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="e">异常对象</param>
        /// <returns></returns>
        public ApiResult FailedResult(int errorCode, string errorMessage, Exception e)
        {
            code = errorCode;
            responseDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            success = false;
            message = errorMessage;
            resultObject = e;
            return this;
        }
    }


    /// <summary>
    /// API返回值数据传输对象（泛型版）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResult<T> : ApiResult
    {
        public virtual T Data { get; set; }

        public virtual ApiResult<T> SetSuccessResult(T t)
        {
            var result = new ApiResult<T>();
            result.SuccessResult().resultObject = t.GetType().Name;
            result.Data = t;
            return result;
        }
    }
}
